<?php

include_once('top.php');

if (isset($_GET['hours_saved'])) {
	$message = "Saved Hours";
}

$user = fetch_only("SELECT First_name, Last_name, Email FROM USER WHERE Email = '$email' LIMIT 1");
$projects = fetch_all("SELECT PROJECT.P_id, PROJECT.Title, Manager FROM PROJECT , PROJECT_WORKERS WHERE (PROJECT.P_id = PROJECT_WORKERS.Project AND PROJECT_WORKERS.Employee = '$email') OR Manager='$email' GROUP BY P_id ORDER BY PROJECT.P_id ASC");
$tasks = fetch_all("SELECT TASK.Name, PROJECT.Title AS Project, TASK.Project AS Pid, TASK.Due_date, TASK.Milestone FROM TASK , PROJECT WHERE TASK.Employee = '$email' AND TASK.Project = PROJECT.P_id ORDER BY TASK.Due_date ASC");

for ($i = 0; $i < count($projects); $i++) {
	if ($projects[$i]['Manager'] === $email) {
		$projects[$i]['Role'] = 'Project Manager';
	} else {
		$projects[$i]['Role'] = 'Team Member';
	}
}

$today = time();

for ($i = 0; $i < count($tasks); $i++) {
	if (strtotime($tasks[$i]['Due_date']) < $today) {
		$tasks[$i]['late'] = true;
	} else {
		$tasks[$i]['late'] = false;
	}
}

format_date($tasks, 'Due_date', 'm-d-y');

for ($i = 0; $i < count($tasks); $i++) {
	$tasks[$i]['Url'] = "time_tracking.php?task={$tasks[$i]['Name']}&project={$tasks[$i]['Pid']}&milestone={$tasks[$i]['Milestone']}";
}

$html = '';

foreach ($projects as $project) {
	if ($project['Role'] == 'Project Manager') {
		$html .= '<a href="project_tasks.php?project=' . $project['P_id'] . '">' . $project['Title'] . '</a><br />';
	}
}

if (empty($html)) {
	$html = 'You do not manage any projects.<br />';
} else {
	$html = 'Your Projects<br />' . $html;
}

?>

<html>
<head>
	<title>GT Project Manager - Dashboard</title>
	<link rel="stylesheet" type="text/css" href="main.css" />
	<link rel="stylesheet" type="text/css" href="inner.css" />
	<style type="text/css">
		a.selected {
			background-color:#1F75CC;
			color:white;
			z-index:100;
		}
		
		.messagepop {
			background-color:#FFFFFF;
			border:1px solid #999999;
			cursor:default;
			display:none;
			margin-top: 15px;
			position:absolute;
			text-align:left;
			width:200px;
			z-index:50;
			padding: 12px;
			margin-top: -12px;
			margin-left: -99px;
		}
		
		.messagepop a {
			text-decoration: none;
		}
		
		label {
			display: block;
			margin-bottom: 3px;
			padding-left: 15px;
			text-indent: -15px;
		}
		
		.messagepop p, .messagepop.div {
			border-bottom: 1px solid #EFEFEF;
			margin: 8px 0;
			padding-bottom: 8px;
		}
	</style>
	<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
	<script type="text/javascript">
		function go(event) {
			$(this).addClass("selected").parent().append('<div class="messagepop pop"><?php echo $html ?><br /><a class="close" href="/">Cancel</a></div>');
			$(".pop").slideFadeToggle(function() { 
				$("#email").focus();
			});
			
			$("#popup").die('click');
			$("#popup").live('click', nothing);
			
			return false;
		}
		
		function nothing() {
			return false;
		}
		
		$(function() {
			$("#popup").live('click', go);
	
			$(".close").live('click', function() {
				$(".pop").slideFadeToggle(function() { 
					$("#popup").removeClass("selected");
				});
				
				$('#popup').die('click');
				$("#popup").live('click', go);
				
				return false;
			});
		});
	
		$.fn.slideFadeToggle = function(easing, callback) {
			return this.animate({ opacity: 'toggle', height: 'toggle' }, "fast", easing, callback);
		};    
	</script>
</head>
<body>
	<table cellspacing="0" cellpadding="0" id="main">
		<tr><td id="title">
			Dashboard for <?php echo "{$user['First_name']} {$user['Last_name']}" ?>
			<div class="nav">
				<a href="login.php?action=logout">Log Out</a><br />
				<a href="project_tasks.php" id="popup">Project Tasks Report</a><br />
				<a href="employee_tasks.php">Employee Tasks Report</a>
			</div>
		</td></tr>
		<?php
		if ($message) {
		?>
		<tr><td class="message" colspan="2"><?php echo $message ?></td></tr>
		<?php
		}
		?>
		<tr>
			<td>
				<table class="subsection summary" cellspacing="0" cellpadding="0">
					<tr><td class="title" colspan="3">
						My Projects
						<hr size="1" />
						<div class="link">
							<a href="project_info.php?manager=<?php echo $user['Email'];?>">New Project</a>
						</div>
					</td></tr>
					<tr>
						<th>ID</th>
						<th>Project Title</th>
						<th>My Role</th>
					</tr>
					<?php
					foreach ($projects as $project) {
					?>
					<tr>
						<td><? echo $project['P_id'] ?></td>
						<?php
						if ($project['Role'] == 'Project Manager') {
						?>
						<td><a href="project_info.php?project=<? echo $project['P_id'] ?>"><? echo $project['Title'] ?></a></td>
						<?php
						} else {
						?>
						<td><? echo $project['Title'] ?></td>
						<?php
						}
						?>
						<td><? echo $project['Role'] ?></td>
					</tr>
					<?php
					}
					?>
				</table>
			</td>
		</tr>
		<tr>
			<td>
				<table class="subsection summary" cellspacing="0" cellpadding="0">
					<tr><td class="title" colspan="4">
						My Tasks
						<hr size="1" />
					</td></tr>
					<tr>
						<th>Task Name</th>
						<th>Project Title</th>
						<th>Due Date</th>
					</tr>
					<?php
					foreach ($tasks as $task) {
					
					if ($task['late']) {
					?>
					<tr class="late">
					<?
					} else {
					?>
					<tr>
					<?
					}
					?>
						<td><a href="<? echo $task['Url'] ?>"><? echo $task['Name'] ?></a></td>
						<td><? echo $task['Project'] ?></td>
						<td><? echo $task['Due_date'] ?></td>
					</tr>
					<?
					}
					?>
				</table>
			</td>
		</tr>
	</table>
</body>
</html>